var ClubRecordTab = Item.extend({
    _ccsFileName: res.ClubRecordTab_json,
    _clubId: null,
    _tableViewList: [],
    _haveNextPage: true,

    _searchUid: 0,           //搜索玩家的UID
    _selectedScoreMin: 0,    //选择的分数档位
    _selectedRecordType: 0,
    _selectedMarkedType: 2,  //是否被标记 0未标记 1标记 2全部
    _selectedMapIdStr: '',
    _selectYear: null,    //日期选择的 年
    _selectMonth: null,   //日期选择的 月
    _selectday: null,     //日期选择的 日

    _currentSelect: 0,
    _currentselectYear: null,    //日期选择的 年
    _currentselectMonth: null,   //日期选择的 月
    _currentselectday: null,     //日期选择的 日

    _currentPage: 0,      //第一页为0  显示到label上时要+1
    _scoreArea: '',        //筛选的分数区间

    _recordItem: null,
    initItem: function (clubId) {
        this._clubId = clubId;
        var today = new Date();

        //初始化筛选条件
        this._scoreArea = '0,999999';
        this._nodeList['dangweiLabel'].setString('分数档位:不限');
        this._selectedScoreMin = 0;
        this._selectedRecordType = 0;
        this._selectedMarkedType = 2;
        this._selectedMapIdStr = ClubDataManager.getAllAppWanfaMapIdStr();
        this._selectYear = today.getFullYear();
        this._selectMonth = today.getMonth() + 1;
        this._selectday = today.getDate();

        this._currentselectYear = this._selectYear;
        this._currentselectMonth = this._selectMonth;
        this._currentselectday = this._selectday;
        
        this._refreshDateView(this._selectYear, this._selectMonth, this._selectday);

        this._tableViewList = [0, 0, 0, 0, 0, 0];
        this._initTableView();
        // this._initDropDownMenu1();
        // this._initDropDownMenu2();
        // this._initDropDownMenu3();
        this._refreshPage();
        this._nodeList['tipsLabel'].setVisible(false);


        // this._recordItem = new ClubRecordItem();
        // this._recordItem.retain();
    },
    _clickDay:function (target) {
        var btnName = target.getName();
        if (btnName == 'btn_upday') {
            this._currentSelect ++;
        } else {
            this._currentSelect --;
        }
        var selectday = new Date(this._currentselectYear+'/'+this._currentselectMonth+'/'+this._currentselectday+' 00:00:00');
        var jiange = selectday.getTime() - 86400000* this._currentSelect;
        var newdate =  new Date(jiange);
        var maxdate =  new Date();
        var mixdate =  new Date(selectday.getTime() - 86400000* 15);
        if(newdate.getTime() > maxdate.getTime() || newdate.getTime() < mixdate){
            if (btnName == 'btn_upday') {
                this._currentSelect --;
            } else {
                this._currentSelect ++;
            }
            return ;
        }
        var newyear = newdate.getFullYear();
        var newmonth  = newdate.getMonth()+1;
        var newday  = newdate.getDate();
        this.refrendate(newyear,newmonth,newday);
    },
    _initClickListeners: function () {
        this._clickListeners = {
            'btn_edit': '_clickEditScore',
            'dateNode': '_clickDateNode',
            'searchNode.bg': '_clickSerarchIdBg',
            'searchNode.btn_search': '_clickSerarchId',
            'searchNode.btn_cancel': '_clickCancelID',
            'btn_upPage': '_clickPage',
            'btn_downPage': '_clickPage',
            'btn_downday': '_clickDay',
            'btn_upday': '_clickDay',
        };
    },

    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        //监听 消息 事件
        this._eventListeners[ClubRecordCode.ClubRecord] = '_clubRecordHandler';
        this._eventListeners[ClubUserEditOverEvent] = '_clubEditHanlder';
        this._eventListeners[ClubRecordCode.RecordDetail] = '_clubRecordDetailHandler';
        this._eventListeners[ClubRecordCode.ClubRecordMarked] = '_clubMaredHandler';
    },

    /**
     * 初始化TableView
     */
    _initTableView: function () {
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        this._tableView = new cc.TableView(this, cc.size(tableViewSize.width, tableViewSize.height));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.setPosition(nodeContent.getPosition());
        nodeContent.getParent().addChild(this._tableView);
    },

    _clubRecordHandler: function (data) {
        this._tableViewList = data;
        this._nodeList['tipsLabel'].setVisible(!(this._tableViewList.length > 0));
        this._tableView.reloadData();
        //如果战绩列表 已经不够一页了。则不能再下翻页
        this._haveNextPage = !(this._tableViewList.length < ClubDataManager.RequestRecordMax);
    },

    _clubEditHanlder: function (data) {
        var leftValue = data['leftValue'] == '0' ? '0' : data['leftValue'];
        var rightValue = data['rightValue'] == '0' ? '0' : data['rightValue'];
        if (rightValue == '0') {
            rightValue = '99999'
        }
        this._nodeList['dangweiLabel'].setString('分数档位:' + leftValue + '-' + rightValue);
        if (leftValue == '0' && rightValue == '0') {
            this._scoreArea = '0,99999';
            this._nodeList['dangweiLabel'].setString('分数档位:不限');
        } else {
            this._scoreArea = leftValue + ',' + rightValue
        }
        this._currentPage = 0;
        this._refreshPage(true);
    },

    /**
     * 初始化下拉菜单
     * @private
     */
    _initDropDownMenu1: function () {
        var config = {
            selectBgRes: res.dropDownMenuTopBack,
            selectBtnRes: res.dropDownMenuBtn,
            menuRes: res.dropDownMenuBack1,
            normalIndex: 0,
            fontSize: 20,
            dataArr: ['总战绩', '大赢家'],
            selectCallBack: this._dropDownMenuHandler1.bind(this)
        };
        this._dropDownMenu = new DropDownMenu(config);
        this._nodeList['menu1'].getParent().addChild(this._dropDownMenu);
        this._dropDownMenu.setPosition(this._nodeList['menu1'].getPosition());
    },

    /**
     * 初始化下拉菜单
     * @private
     */
    _initDropDownMenu2: function () {
        var config = {
            selectBtnRes: res.dropDownMenuBtn,
            menuRes: res.dropDownMenuBack2,
            normalIndex: 0,
            fontSize: 20,
            selectBgRes: res.dropDownMenuTopBack,
            dataArr: _.keys(club.ClubWanfaFilter),
            selectCallBack: this._dropDownMenuHandler2.bind(this)
        };
        this._dropDownMenu = new DropDownMenu(config);
        this._nodeList['menu2'].getParent().addChild(this._dropDownMenu);
        this._dropDownMenu.setPosition(this._nodeList['menu2'].getPosition());
    },

    /**
     * 初始化下拉菜单
     * @private
     */
    _initDropDownMenu3: function () {
        var config = {
            selectBgRes: res.dropDownMenuTopBack,
            selectBtnRes: res.dropDownMenuBtn,
            menuRes: res.dropDownMenuBack2,
            normalIndex: 2,
            fontSize: 20,
            dataArr: ['未标记', '已标记', '全部'],
            selectCallBack: this._dropDownMenuHandler3.bind(this)
        };
        this._dropDownMenu = new DropDownMenu(config);
        this._nodeList['menu3'].getParent().addChild(this._dropDownMenu);
        this._dropDownMenu.setPosition(this._nodeList['menu3'].getPosition());
    },


    _dropDownMenuHandler1: function (content, index) {
        this._selectedRecordType = index;
        this._currentPage = 0;
        this._refreshPage(true);
        // this._changeFilter();
    },

    _dropDownMenuHandler2: function (content, index) {
        if (content == '所有玩法') {
            this._selectedMapIdStr = ClubDataManager.getAllAppWanfaMapIdStr();
        } else {
            this._selectedMapIdStr = club.ClubWanfaFilter[content].join(',');
        }
        this._currentPage = 0;
        this._refreshPage(true);
        // this._changeFilter();
    },

    _dropDownMenuHandler3: function (content, index) {
        this._selectedMarkedType = index;
        this._currentPage = 0;
        this._refreshPage(true);
        // this._changeFilter();
    },

    /***
     * 时期选择回调
     * @param year
     * @param month
     * @param day
     */
    _powerDateHandler: function (year, month, day) {
        this._currentSelect = 0;
        this._currentselectYear = year;
        this._currentselectMonth = month;
        this._currentselectday = day;

        this.refrendate(year,month,day);
    },
    refrendate:function (year,month,day) {
        this._refreshDateView(year, month, day);
        this._currentPage = 0;
        this._refreshPage(true);
    },
    /**
     * 编辑面板回调
     * @param str  内容
     * @private
     */
    _editPanelCallBack: function (str) {
        this._selectedScoreMin = parseInt(str);
        $('dangweiLabel').setString('分数档位:' + this._minScoreFilter);
        this._filterRecord();
    },

    _clickEditScore: function () {
        showWindow('ClubUserEditScoreWindow');
    },
    _clickDateNode: function () {
        var today = new Date();
        var curyear = today.getFullYear();
        var curmonth = today.getMonth() + 1;
        var curday = today.getDate();
        var _clubPowerDate = new ClubPowerDate(curyear, curmonth, curday, this._powerDateHandler.bind(this));
        _clubPowerDate.x = -400;
        _clubPowerDate.getChildByName('Node').getChildByName('modalLayer').setOpacity(0);
        this.addChild(_clubPowerDate);
    },

    _clickSerarchIdBg: function () {
        showWindow('ClubInputWindow', '请输入玩家Id', ClubInputWindowType.InputID, this._searchUidHandler.bind(this));
    },
    /**
     * 输入 uid后确定回调
     * @param inputStr
     * @private
     */
    _searchUidHandler: function (inputStr) {
        this._searchUid = inputStr;
        if (this._searchUid == undefined || this._searchUid == "") {
            this._nodeList['searchNode.tipsLabel'].setString('输入用户ID搜索');
            return;
        }
        var m_AllNum = /^[0-9]\d*$/.test(this._searchUid);
        if (!m_AllNum) {
            showAlert('请输入数字!');
            return;
        }
        this._nodeList['searchNode.tipsLabel'].setString(this._searchUid);
    },

    _clickSerarchId: function () {
        this._currentPage = 0;
        this._refreshPage(true);
    },
    _clickCancelID: function () {
        this._searchUid = 0;
        this._nodeList['searchNode.tipsLabel'].setString('输入用户ID搜索');
        this._currentPage = 0;
        this._refreshPage(true);
    },

    _clickPage: function (target) {
        var btnName = target.getName();
        if (btnName == 'btn_upPage') {
            this._currentPage--;
        } else {
            if (!this._haveNextPage) {
                showToast('已经是最后一页');
                return;
            }
            this._currentPage++;
        }
        if (this._currentPage < 0) {
            this._currentPage = 0;
            return;
        }
        this._refreshPage(true);
    },

    _refreshPage: function (isReqeust) {
        this._nodeList['pageLabel'].setString((this._currentPage + 1));
        if (isReqeust) {
            this._changeFilter();
        }
    },

    /**
     * 刷新界面显示
     */
    refreshView: function () {
        this._haveNextPage = true;
        this._changeFilter();
    },
    /**
     * 刷新时间显示
     * @private
     */
    _refreshDateView: function (year, month, day) {
        this._selectYear = year;
        this._selectMonth = month;
        this._selectday = day;
        if (this._selectMonth < 10) {
            this._selectMonth = '0' + this._selectMonth;
        }
        if (this._selectday < 10) {
            this._selectday = '0' + this._selectday;
        }
        this._nodeList['dateNode.dateLabel'].setString(this._selectYear + '/' + this._selectMonth + '/' + this._selectday);
    },

    _changeFilter: function () {
        var date = this._selectYear + '-' + this._selectMonth + '-' + this._selectday;
        // clubId, mapIds, playerId, date_from, date_to, marked, win, socre, page
        clubMsgManager.requestClubRecord(this._clubId,
            this._selectedMapIdStr,
            this._searchUid,
            date,
            date,
            this._selectedMarkedType,
            this._selectedRecordType,
            this._scoreArea,
            this._currentPage);
    },

    _clubRecordDetailHandler: function (data) {
        ClubDataManager.ClubRecordData = data;
        club.openHistoryDetail(this, data);
    },

    _clubMaredHandler: function () {
        this._changeFilter();
    },

    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            // node = duplicateNode(this._recordItem);
            node = addItem('ClubRecordItem', cell, idx, this._tableViewList[idx]);
            node.setName("itemCell");
            // cell.addChild(node);
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(idx, this._tableViewList[idx]);
        }
        return cell;
    },

    tableCellTouched: function (table, cell) {
        var recordInfo = cell.getChildByName("itemCell").getRecordInfo();
        clubMsgManager.requestRecordDetail(recordInfo['map_id'], gameData.uid, recordInfo['mongo_id']);
        // cc.log('数据:' + JSON.stringify(recordInfo))
        // var idx = cell.getIdx();
        // var itemData = this._tableViewList[idx];
        // if (itemData && itemData['result_arr'] && itemData['result_arr'].length > 0) {
        //     ClubDataManager.ClubRecordData = itemData;
        //     club.openHistoryDetail(this, itemData);
        // } else {
        //     showAlert("尚未产生战绩");
        // }
    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(996, 151);
    },
    numberOfCellsInTableView: function (table) {
        return this._tableViewList.length;
    },
    // onExit: function () {
    //     this._super();
    //     if (this._recordItem) {
    //         this._recordItem.release();
    //     }
    // },
});